<?php
/**
 * Created by PhpStorm.
 * User: Yooke
 * Date: 2016/5/25
 * Time: 18:06
 */
namespace App\Http\Controllers\Mobile;

use App\Interfaces\CartService;
use App\Interfaces\CommentService;
use App\Interfaces\DiscountService;
use App\Interfaces\GoodsService;
use App\Interfaces\ProductSkuService;
use App\Interfaces\ShopService;
use App\Interfaces\PictureService;
use App\Interfaces\UserCollectionService;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Models\Order;
use App\Models\Goods;
use App\Models\OrderGoods;

use Session,Log;

class GoodsController extends Controller
{

    public function index( $id,Request $request,UserCollectionService $collectionService,GoodsService $goodsService,ShopService $shopService,ProductSkuService $productSkuService,CartService $cartService){
        //dd($id);
        $user = Auth::user();
        $goodsCollectionStatus = 0;
        $address = array();
        if($user){
            $address = $goodsService->getDefaultAddress($user->id);
        }

        $goods = $goodsService->get($id);
        if(!$goods){
            return view('errors.mobile404');
        }
        $shop = $shopService->get($goods->shop_id);
        $goodsGallery = $goodsService->getGoodsGallery($id);
        $productSku = $productSkuService->getByGoodsId($id);
        $productAttributeArr = $productSku['product_attribute_arr'];
        $cartNumber = $cartService->getCartNumber();

        if($user){
            $user_id = $user->id;
            $userCollection = $collectionService->isInCollection($user_id,"GOODS",$id);
            if($userCollection){
                $goodsCollectionStatus = 1;
            }
        }
//dd($goodsCollectionStatus);

        return view('mobile.goods.index',compact('goods','goodsGallery','shop','address','productAttributeArr','cartNumber','goodsCollectionStatus'));
    }

    public function detail(Request $request, $id)
    {
        return view('mobile.goods.detail', compact(['id']));
    }

    public function show(Request $request, $id)
    {
        return view('mobile.goods.detail', compact(['id']));
    }

    public function comment($id,CommentService $commentService,Request $request){
        $comments = $commentService->getComment($id,"allComment",1);
        $commPercent=$commentService->countComment($id)==0?100:number_format((($commentService->countGoodComment($id))/($commentService->countComment($id))*100),0);
        return view('mobile.goods.comment', compact(['id','comments','commPercent']));
    }

    public function getComment(Request $request,CommentService $commentService,PictureService $pictureService){

        $goods_id = $request->get('goods_id');
        $condition = $request->has('condition')?$request->get('condition'):"allcomment";
        $page = $request->get('page');

        $comments=$commentService->getComment($goods_id,$condition,$page);

        if(empty($comments)){
            return Response()->json(['message'=>'暂无评价','status'=>'0']);
        }
        $data=$comments->toArray()['data'];

        $paginate =  new LengthAwarePaginator($data, count($data), 4, $page, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => 'page',
        ]);
        $commentArr = $paginate->toArray();
        //dd($data['data']);
        return Response()->json(['comments'=>$commentArr,'status'=>'1']);

    }

    public function getRedirectGoods($goods_id,Request $request,GoodsService $goodsService,ProductSkuService $productSkuService,DiscountService $discountService){
        $attributValueArr = $request->attributValueArr;
        $redirectGoods = $goodsService->mobileAttributValueRedirect($goods_id,$attributValueArr);

        if($redirectGoods==-1){
            $goods_info = [];
            $goods_info['goods_id'] =$redirectGoods;
            $goods_info['goods_name'] = "";
            $goods_info['thumb'] = "";
            $goods_info['shop_price'] = "";
            $goods_info['goods_number'] = 0;
            $goods_info['goods_real_price'] = '';

            $data = [
                'goods_info'=>$goods_info
            ];
        }else{
            $goods = Goods::find($redirectGoods);
            $goods_id = $goods->goods_id;

            $productSku = $productSkuService->getByGoodsId($goods_id);

            $goods_info = [];
            $goods = $goodsService->get($goods_id);
            $goods_info['goods_id'] = $goods->goods_id;
            $goods_info['goods_name'] = $goods->goods_name;
            $goods_info['thumb'] = $goods->mobile_goods_thumb;
            $goods_info['shop_price'] = $goods->shop_price;
            $goods_info['goods_number'] = $goods->goods_number;
            $goods_info['goods_real_price'] = $goods->goods_real_price;
            $data = [
                'goods_info'=>$goods_info,
                'product_sku'=>$productSku
            ];
        }

        return response()->json($data);
    }


	/*
	 * 订单商品快照
	 */
	public function goodsArchive($order_sn, $goods_id){

		$order_id = Order::where('order_sn', $order_sn)->pluck('order_id');
		//$goods_id = Goods::where('goods_sn', $goods_sn)->pluck('goods_id');

		$order_goods = OrderGoods::where('order_id', $order_id)->where('goods_id', $goods_id)->first();

		$user_id = Auth::user()->id;

		//还差页面
		return view('mobile.goods.archive', compact('order_goods', 'user_id'));
	}//end func tradeArchive()


    /**
     * 商品可以领取的优惠券页面
     * @param  [type] $goods_id [description]
     * @return [type]           [description]
     */
    public function coupon($goods_id)
    {

        return view('mobile.goods.coupon', compact('goods_id'));
    }


    public function goodsRedirect($goods_sn)
    {

        $goods_id = Goods::where('goods_sn', $goods_sn)->pluck('goods_id');

        return redirect('http://'.env('MOBILE_DOMAIN','m.afuyigou.com').'/goods/show/'.$goods_id);
    }

}
